/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Yudisium;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import library.Koneksi;

/**
 *
 * @author mr2
 */
public class YudisiumModel {
    
    Connection koneksi;
    Yudisium yudisium;
    
    public YudisiumModel(){
        koneksi = Koneksi.getKoneksi();
    }
    
    public List<Yudisium> ListYudisium(int limit){
        
        PreparedStatement prepare = null;
        ResultSet result = null;
        List<Yudisium> listYudisium = new ArrayList<Yudisium>();

        
        try{
            if(limit == 0){
                String sql = "select * from yudisium order by periode desc";
                prepare = koneksi.prepareStatement(sql);
            
                result = prepare.executeQuery();
            }else{
                String sql = "select * from yudisium order by periode desc limit 0,?";
                prepare = koneksi.prepareStatement(sql);
                
                prepare.setInt(1, limit);
            
                result = prepare.executeQuery();
            }
            
                       
            while(result.next()){
                
                Yudisium yudisium = new Yudisium();
                
                yudisium.setPeriode(result.getString("periode"));
                yudisium.setTglMulai(result.getString("tglMulai"));
                yudisium.setTglAkhir(result.getString("tglAkhir"));
                listYudisium.add(yudisium);
                
            }
            return listYudisium;
            
        }catch(SQLException ex){          
            System.out.println(ex.getMessage());
            return listYudisium;
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
    
    //Model insert pendaftaran sidang Proposal
    public boolean InputYudisium(Yudisium yudisium){
        
        boolean hasil = true;
        PreparedStatement prepare = null;
        try{
            String sql = "INSERT INTO yudisium (periode, tglMulai, tglAkhir) values(?,?,?)";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setString(1, yudisium.getPeriode());
            prepare.setString(2, yudisium.getTglMulai());
            prepare.setString(3, yudisium.getTglAkhir());
            
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            System.out.println(hasil);
            
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
        return hasil;
    }
    
    public Yudisium DetailYudisium(Yudisium yudisium){
        PreparedStatement prepare = null;
        ResultSet result = null;
        
        try{
            String sql = "SELECT * from yudisium where periode = ?";
            
            prepare = koneksi.prepareStatement(sql);

            prepare.setString(1, yudisium.getPeriode());
            
            result = prepare.executeQuery();
            
            if(result.next()){
                yudisium = new Yudisium();
                
                yudisium.setPeriode(result.getString("periode"));
                yudisium.setTglMulai(result.getString("tglMulai"));
                yudisium.setTglAkhir(result.getString("tglAkhir"));
            }
            
        }catch(SQLException ex){
            System.out.println(ex.getMessage());
            
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    
                }
            }
        
        }
        return yudisium;
    }
    
    public boolean UpdateYudisium(Yudisium yudisium, String periodeBaru){
        
        PreparedStatement prepare = null;
        boolean hasil = true;
        
        try{
            String sql = "update yudisium set periode = ?, tglMulai = ?, tglAkhir = ? where periode = ? ";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setString(1, periodeBaru);
            prepare.setString(2, yudisium.getTglMulai());
            prepare.setString(3, yudisium.getTglAkhir());
            prepare.setString(4, yudisium.getPeriode());
            
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    System.out.println(ex.getMessage());
                }
            }
        }
        return hasil;
    }
    
   //Model melakukan update bimbingan mahasiswa
   public boolean HapusYudisium(Yudisium yudisium){
        PreparedStatement prepare = null;
        boolean hasil = true;
        try{            
            
            String sql = "delete from yudisium where periode = ?";
            
            prepare = koneksi.prepareStatement(sql);
            
            prepare.setString(1, yudisium.getPeriode());
            int status = prepare.executeUpdate();
            if(status > 0){
                hasil = true;
            }else{
                hasil = false;
            }
            
        }catch(SQLException ex){
            hasil = false;
            System.out.println(ex.getMessage());
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){                   
                }
            }
        }
        
        return hasil;
   }
}
